<?php
//
defined('BY_JYA') or exit('error');
//cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$order_types = array(
		'1'=>'堂点',
		'2'=>'外卖',
		'3'=>'自取',
);
if($_RQ['op']=='main'){
	if(intval($_RQ['store_id'])){
		$store_id = intval($_RQ['store_id']);
		$this->session('store_id',$store_id);
	}
	if(!$store_id){
		$store_id=$this->session('store_id');
	}
	if(!$store_id){
		exi('请先选择门店！','error','jy_dish/index/');
	}
	$order_type=$this->session('order_type');
	$carts=$this->session('carts');
	$goodsprice = 0;
	if($carts){
		foreach($carts as $k=>&$v){
			$allnum += $v['num'];
			$goodinfo = pdo_get('jy_dish_goods',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$k));
			$v['packageinfo']=$goodinfo['package']?json_decode($goodinfo['package'],true):array();
			if(!empty($v['packageinfo'])){
				$goodprice = 0;
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$goodinfo['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
					$goodsprice += $p['num']*$p['price'];
					$goodprice +=$p['num']*$p['price'];
				}	
			}else{
				$goodsprice += $v['num']*$goodinfo['price'];
				$goodprice =$v['num']*$goodinfo['price'];
			}
			$v['goodname']=$goodinfo['title'];
			$v['goodprice']=$goodprice;
			$v['goodnum']=$v['num'];
		}
	}
	$store=pdo_get('jy_dish_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	$youhui = 0;
	if($order_type == 1){
		$table_id=$this->session('table_id');
		$table = pdo_get('jy_dish_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table_id),array('title','id','group_id'));
		$table_group = pdo_get('jy_dish_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$table['group_id']),array('title','id'));
		$return['table_id']=$table_id;
		$return['tablename']=$table['title'].'('.$table_group['title'].')';
		$allprice = $goodsprice - $youhui;
	}elseif($order_type == 2){
		if($goodsprice >= $store['out_end']){
			$youhui = $store['out_pay'];
		}
		$addressinfo=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
		$addressinfo['pcd']=json_decode($addressinfo['pcd'],true);
		$return['addressinfo']=$addressinfo;
		$out_time = out_times($store_id);
		$timestamp = TIMESTAMP + 60 * 30;
		$data = array_order($timestamp, $out_time['timestamp']);
		$predict_index = array_search($data, $out_time['timestamp']);
		$predict_day = $out_time['days'][0];
		$predict_time = $out_time['times'][$predict_index]['start'].'~'.$out_time['times'][$predict_index]['end'];
		$text_time = "尽快送达";
		$return['timestamp']=$timestamp;
		$return['predict_time']=$predict_time;
		$return['out_time']=$out_time;
		$allprice = $goodsprice + $store['out_pay'] - $youhui;
	}elseif($order_type == 3){
		$allprice = $goodsprice - $youhui;
	}elseif($order_type == 5){
		$tables = pdo_getall('jy_dish_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('title','id'));
		foreach($tables as &$t){
			$t['table_list']=pdo_getall('jy_dish_table',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1,'group_id'=>$t['id']));
		}
		$return['tables']=$tables;
		$allprice = $goodsprice - $youhui;
	}
	$return['order_typename']=$order_types[$order_type];
	$return['order_type']=$order_type;
	$return['carts']=$carts;
	$return['store']=$store;
	$return['allprice']=$allprice;
	$return['youhui']=$youhui;
	set_html('title','订单详情');
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='selecttable'){
	$store_id=$this->session('store_id');
	$return['tables']=pdo_getall('jy_dish_table',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1,'group_id'=>$_RQ['group_id']));
	$return['success']=1;
	exi($return);
}
	//提交订单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择门店！','error','jy_dish/index/');
	}	
	$store=pdo_get('jy_dish_store',array('id'=>$store_id));
	$youhui = 0;
	$carts=$this->session('carts');
	if(!$carts){
		exi('没有选择商品！','error');
	}
	$total=0;
	foreach($carts as $k=>&$v){
			$allnum += $v['num'];
			$goodinfo = pdo_get('jy_dish_goods',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$k));
			$v['packageinfo']=$goodinfo['package']?json_decode($goodinfo['package'],true):array();
			if(!empty($v['packageinfo'])){
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$goodinfo['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
					$total += $p['num']*$p['price'];
				}	
			}else{
				$total += $v['num']*$goodinfo['price'];
			}
	}
	$addressinfo=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	$memberinfo=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$member = pdo_get('jy_dish_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(empty($member)){
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'name'=>$memberinfo['nickname'],
			'mobile'=>$memberinfo['mobile'],
			'pcd'=>$addressinfo['pcd'],
			'address'=>$addressinfo['address'],
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_dish_member',$data);
	}
	if($this->session('order_type')==1){
		$table=pdo_get('jy_dish_table',array('id'=>$this->session('table_id')));
		pdo_update('jy_dish_table',array('busy'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('table_id')));
		$price=$store['in_pay']*$post['user_num']+$total - $youhui;	
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'order_type'=>1,
			'ordersn'=>get_ordersn('jy_dish_order','ordersn'),
			'price'=>$price,
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_dish_order',$data);
		$orderid=pdo_insertid();
		$this->session('insertid',$orderid);
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'user_num'=>$post['user_num'],
			'user_fee'=>$store['in_pay'],
			'table_id'=>$this->session('table_id'),
			'order_id'=>$orderid
		);
		pdo_insert('jy_dish_order_table',$data);
	}
	if($this->session('order_type')==2){
		$lng=json_decode($store['lat_lng'],true);
		$out_distance=$store['out_distance'];
		$user_lng=$post['receiver_latng'];
		if($user_lng){
			$distance=cfc('tools')->getdistance($lng['lng'],$lng['lat'],$user_lng['lng'],$user_lng['lat']);
			if($distance>$out_distance){
				exi('超出配送距离','error');
			}
		}
		if($store['out_end']<$total){
			$price=$total - $youhui;
		}else{
			$price=$total+$store['out_pay'] -$youhui;	
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'order_type'=>2,
			'ordersn'=>get_ordersn('jy_dish_order','ordersn'),
			'price'=>$price,
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_dish_order',$data);
		$this->session('insertid',pdo_insertid());
		$addressinfo['pcd']=json_decode($addressinfo['pcd'],true);
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'order_id'=>$this->session('insertid'),
			'delivery_fee'=>$store['out_pay'],
			'receiver_name'=>$addressinfo['name'],
			'receiver_mobile'=>$addressinfo['mobile'],
			'receiver_address'=>$addressinfo['pcd']['province'].$addressinfo['pcd']['city'].$addressinfo['pcd']['district'].$addressinfo['address'],
			'receiver_latng'=>$post['receiver_latng'],
			'out_time'=>ijson_encode($post['out_time']),
		);
		pdo_insert('jy_dish_order_out',$data);
	}
	if($this->session('order_type')==3){
		$price=$total - $youhui;
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'order_type'=>3,
			'ordersn'=>get_ordersn('jy_dish_order','ordersn'),
			'price'=>$price,
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_dish_order',$data);
		$orderid=pdo_insertid();
		$this->session('insertid',$orderid);
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'order_id'=>$this->session('insertid'),
			'username'=>$addressinfo['name'],
			'mobile'=>$addressinfo['mobile'],
			'selftime'=>ijson_encode($post['selftime']),
		);
		pdo_insert('jy_dish_order_self',$data);
		$id = pdo_insertid();
		$selfnumber=sprintf("%05d", $id);
		pdo_update('jy_dish_order_self',array('selfnumber'=>$selfnumber),array('id'=>$id));
	}
	foreach($carts as $k=>&$v){
			$str='';
			$good=pdo_get('jy_dish_goods',array('id'=>$k));
			$v['packageinfo']=$good['package']?json_decode($good['package'],true):array();
			if(!empty($v['packageinfo'])){
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$good['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
				}
			}
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'store_id'=>$store_id,
				'order_id'=>$this->session('insertid'),
				'goods_id'=>$k,
				'goods_num'=>$v['num'],
				'goods_price'=>$good['price'],			
				'goods_title'=>$good['title'],
				'goods_package'=>ijson_encode($v['packageinfo']),
			);
			pdo_insert('jy_dish_order_goods',$data);
	}
	$this->session('carts','');
	$order_typek = $this->session('order_type');
	$order_type=$this->type__order();
	$order_type=$order_type[$order_typek];
	//发信息
	if($this->session('order_type')==1 || $this->session('order_type')==2){
		$this->notice('order_new',$_SESSION['uid'],$store_id,$this->session('insertid'));
	}elseif($this->session('order_type')==3){
		$this->notice('salf_order_new',$_SESSION['uid'],$store_id,$this->session('insertid'));
	}
	//打印
	$this->printmsg($text,$this->session('insertid'),0);
    $params['fee']     = $price;
    $params['title']   = $order_type.'订单';
	$params['id']=$this->session('insertid');
	$params['plugin']='jy_dish';
	$params['no_type']=array('hdfk');//可选，此项作用为指定不允许使用哪种支付方式
	$params['back_url']='jy_dish/order/payresult/';
	$pay_id=cfc('pay')->create_pay_order($params);
	pdo_update('jy_dish_order',array('pay_id'=>$pay_id),array('id'=>$params['id']));
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']==1){
		$order=pdo_get('jy_dish_order',array('id'=>$payresult['id']));
		if($order['is_pay'] ==1){
			exi('该订单已经支付！','error','jy_dish/order_show/');
		}
		if($order['price']==$payresult['fee']){
			pdo_update('jy_dish_order',array('status'=>1,'pay_type'=>$payresult['pay_type'],'is_pay'=>1),array('id'=>$payresult['id']));
			pdo_update('jy_dish_table',array('busy'=>3),array('id'=>$order['table_id']));
			//发信息
			$this->notice('order_pay',$_SESSION['uid'],$store_id,$payresult['id']);
			//打印
			$this->printmsg($text,$order['id'],1);
			exi('支付成功!','success','jy_dish/order_show/');
		}else{
			exi('支付失败！','error','jy_dish/order/');
		}
	}else{
		exi('支付失败。','error','jy_dish/order/');
	}
}
if($_RQ['op']=='selecttime'){
	$store_id=$this->session('store_id');
	$out_time = out_times($store_id);
	$timestamp = TIMESTAMP + 60 * 30;
	$return['timestamp']=$timestamp;
	$return['out_time']=$out_time;
	$return['success']=1;
	exi($return);
}
function out_times($store_id) {
		$store=pdo_get('jy_dish_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
		//配送时间
		$days = array();
		$totaytime = strtotime(date('Y-m-d'));
		$days[] = date('m-d');

		//配送时间段
		$timestamp = array();
		$start = mktime(8, 0);
		$end = mktime(22, 0);
		for($i = $start; $i < $end;) {
				$times[] = array(
					'start' => date('H:i', $i),
					'end' => date('H:i', $i + 1800),
					'timestamp' => $i + 1800,
					'fee' => 0,
					'timev' =>date('H:i', $i).'~'.date('H:i', $i + 1800),
					'out_pay' => $store['out_pay']."元配送费",
				);
				$timestamp[] = $i + 1800;
				$i += 1800;
		}
		$data = array(
			'days' => $days,
			'times' => $times,
			'timestamp' => $timestamp,
			'updatetime' => strtotime(date('Y-m-d')) + 86400,
			'reserve' => 0,
		);
		return $data;
}
function array_order($value, $array)
{
    $array[] = $value;
    asort($array);
    $array = array_values($array);
    $index = array_search($value, $array);
    return $array[$index + 1];
}
?>